<script setup>
import {
  Search,
  Refresh,
  Plus,
  Delete,
  Download,
  Edit,
  UserFilled
} from '@element-plus/icons-vue'
import { suerinfoGetService } from '@/api/admin/user'
import { ref } from 'vue'
import diaLog from '@/views/admin/components/dialog.vue'
import { utils, writeFile } from 'xlsx'
const table = ref(null)
const userinfoview = ref([])
const userinfo = ref([])
const loading = ref(false)
const dialog = ref()
const keyname = ref()
const keyphone = ref()
const selectedRows = ref([])
const delectDisabled = ref(true)
const handleSelectionChange = (selectVal) => {
  delectDisabled.value = false
  selectedRows.value = selectVal
}
//关键字搜索
const keySearch = () => {
  userinfoview.value = []
  userinfoview.value = userinfo.value.filter((row) => {
    if (keyname.value && !keyphone.value) {
      if (row.name.includes(keyname.value)) {
        return true
      } else {
        return false
      }
    } else if (!keyname.value && keyphone.value) {
      if (row.phone.endsWith(keyphone.value)) {
        return true
      } else {
        return false
      }
    } else {
      if (
        row.name.includes(keyname.value) &&
        row.phone.endsWith(keyphone.value)
      ) {
        return true
      } else {
        return false
      }
    }
  })
}
const resetFilter = () => {
  userinfoview.value = userinfo.value
  keyname.value = ''
  keyphone.value = ''
}
const getUserInfoList = async () => {
  loading.value = true
  const res = await suerinfoGetService()
  userinfo.value = res.data.data
  userinfoview.value = res.data.data
  loading.value = false
}
getUserInfoList()
//点击编辑的

const onEditUserinfo = (row) => {
  dialog.value.open(row)
  //编辑操作
}
const addUserinfo = () => {
  //添加操作
  dialog.value.open({})
}
//导出数据
const exportExcel = () => {
  if (table.value) {
    const sheet = utils.table_to_sheet(table.value.$el)
    const workbook = utils.book_new()
    utils.book_append_sheet(workbook, sheet, 'Sheet1')
    writeFile(workbook, 'userinfo_data.xlsx')
  }
}
//修改重新渲染
const onSuccess = () => {
  getUserInfoList()
}
import { userinfoDlService } from '@/api/admin/user'
const deletUserinfo = async (row) => {
  await ElMessageBox.confirm('是否删除该记录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true
  })
  userinfoDlService(row.id)
    .then((response) => {
      if (response.data.status == 200) {
        ElMessage.success(response.data.message)
        getUserInfoList()
      } else {
        ElMessage.error(response.data.message)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
</script>

<template>
  <page-container title="用户信息">
    <!-- 头部 -->
    <template #extra
      ><strong>用户名</strong>
      <el-input
        placeholder="请输入用户名"
        style="width: 110px"
        v-model="keyname"
      />
      <strong>手机号码</strong>
      <el-input
        placeholder="请输入手机号连续后几位"
        style="width: 180px"
        v-model="keyphone"
      />
      <el-button :icon="Search" @click="keySearch">搜索</el-button>
      <el-button :icon="Refresh" @click="resetFilter">重置</el-button>
      <el-button type="primary" plain :icon="Plus" @click="addUserinfo"
        >新增</el-button
      >

      <el-button type="warning" plain :icon="Download" @click="exportExcel"
        >导出</el-button
      >
    </template>
    <el-table
      :data="userinfoview"
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="name" label="用户名" width="120" />
      <el-table-column prop="phone" label="手机号" width="120" />
      <el-table-column prop="address" label="地址" width="200" />
      <el-table-column prop="sex" label="性别" width="120" />
      <el-table-column prop="age" label="年龄" width="120" />
      <el-table-column prop="avatar" label="头像" width="120">
        <template v-slot="scope">
          <el-avatar
            v-if="scope.row.avatar == '' || scope.row.avatar == null"
            :icon="UserFilled"
          />
          <el-avatar v-else :src="scope.row.avatar" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            size="small"
            circle
            plain
            @click="onEditUserinfo(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            circle
            plain
            @click="deletUserinfo(row, $index)"
          ></el-button>
        </template>
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <diaLog ref="dialog" @success="onSuccess"></diaLog>
  </page-container>
</template>
<style></style>
